package com.lepu.app.main.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.core.lib.adapter.ViewPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.viewpager.RotateDownPageTransformer;
import com.core.lib.utils.viewpager.ViewPagerCompat;
import com.core.lib.utils.viewpager.ViewPagerCompat.OnPageChangeListener;
import com.eyzhs.app.R;

/*
 * 程序引导页
 */
public class AppStartActivityGuide extends BaseFragmentActivity {

    private ViewPagerCompat mHelpViewPager = null;
    private Button mGoButton = null;

    private static final int PAGE = 4;
    private LinearLayout mFocusIndicator;

    @Override
    public void onCreate(Bundle savedInstanceState) {

    	mNeedSwipeBack = false;
		mNeedStatusBar = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_start_activity_guide);

        init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    /**
     * 初始化方法
     */
    @SuppressLint("InflateParams")
    private void init() {

        mFocusIndicator = (LinearLayout) findViewById(R.id.focusIndicator);

        LayoutParams params = new LinearLayout.LayoutParams(
                20, 20);
        params.setMargins(4, 4, 4, 4);

        for (int i = 0; i < PAGE; i++) {
            View v = new View(this);
            v.setBackgroundResource(R.drawable.app_guide_point_bg);
            if (i == 0) {
                v.setEnabled(true);
            } else {
                v.setEnabled(false);
            }
            mFocusIndicator.addView(v, params);
        }

        View view1 = getLayoutInflater().inflate(
                R.layout.app_start_guide_view1, null);
        View view2 = getLayoutInflater().inflate(
                R.layout.app_start_guide_view2, null);
        View view3 = getLayoutInflater().inflate(
                R.layout.app_start_guide_view3, null);
        View view4 = getLayoutInflater().inflate(
                R.layout.app_start_guide_view4, null);

        ArrayList<View> viewList = new ArrayList<View>();
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);

        mHelpViewPager = (ViewPagerCompat) findViewById(R.id.helpViewPager);
        mHelpViewPager.setPageTransformer(true, new RotateDownPageTransformer());
        mHelpViewPager.setAdapter(new ViewPageAdapter(viewList, null, null));
        mHelpViewPager.setOnPageChangeListener(new MyViewChangeListener());
        mHelpViewPager.setCurrentItem(0);

        // go
        mGoButton = (Button) findViewById(R.id.goButton);
        mGoButton.setVisibility(View.GONE);
        mGoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            	
            	finishAnim(0,0);
            	
            	// 进入主程序
				Intent intentMain = new Intent(AppStartActivityGuide.this,
						MainTabActivity.class);
				startActivityAnim(intentMain,0,0);
            }
        });

    }

    private class MyViewChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int index) {
            mHelpViewPager.setCurrentItem(index);

            if (index == PAGE - 1) {
                mGoButton.setVisibility(View.VISIBLE);
            } else {
                mGoButton.setVisibility(View.GONE);
            }

            for (int i = 0; i < mFocusIndicator.getChildCount(); i++) {
                mFocusIndicator.getChildAt(i).setEnabled(false);
            }

            mFocusIndicator.getChildAt(index).setEnabled(true);
        }
    }

}
